
package com.hulk.ratel.web.control;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.hulk.ratel.common.constant.enums.BussErrCode;
import com.hulk.ratel.common.exception.WebRteException;
import com.hulk.ratel.common.tool.ValidatorTool;
import com.hulk.ratel.common.util.DateUtil;

import com.hulk.ratel.manage.commonutil.ContentUtil;
import com.hulk.ratel.service.dto.BaseHeadReqDTO;
import com.hulk.ratel.service.dto.BaseHeadRespDTO;
import com.hulk.ratel.service.dto.HeadRespDTO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestAttribute;

import java.io.IOException;

/**
 * @author hulk
 * @version Create on: 2017年5月1日 下午7:05:05 Class description
 * @E-mail:29572320@qq.com
 */

@Slf4j
@Component
public abstract class AbsBaseCtl {


    @Autowired
    protected ObjectMapper objectMapper;
    @Autowired
    protected ContentUtil contentUtil;



    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", paramType = "query", name = "partnerNo", value = "合作方编号", required = true),
            @ApiImplicitParam(dataType = "String", paramType = "query", name = "traceId", value = "合作方交易流水号", required = true) ,
            @ApiImplicitParam(dataType = "String", paramType = "query", name = "plainText", value = "报文", required = true),
            @ApiImplicitParam(dataType = "String", paramType = "query", name = "partnerAESKey", value = "AES秘钥", required = true) ,
    })
    /*@ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 500, message = "处理错误") })*/
    @NonNull
    public abstract String excute(
            @RequestAttribute("partnerNo") String partnerNo,
            @RequestAttribute("traceId") String traceId,
            @RequestAttribute("plainText") String plainText,
            @RequestAttribute("partnerAESKey") String partnerAESKey,
            @RequestAttribute("intCutDate") String intCutDate,
            @RequestAttribute("workId") String workId);

    @SneakyThrows
    private <T> T jsonParse(String plainText, String workId, Class<T> clazz)  {
        log.debug("AbsTxnSwitch.jsonParse");
        return objectMapper.readValue(plainText, clazz);

    }

    private String validateBean(Object o, String workId) {
        String validateResult = ValidatorTool.validateBean(o);
        if (!Strings.isNullOrEmpty(validateResult)) {
            throw new WebRteException(workId, BussErrCode.E_100001.getErrCode(), validateResult);
        }
        return validateResult;
    }



    protected <T> T preHandle(@NonNull String plainText, @NonNull String workId, String validateResult, @NonNull Class<T> clazz) {
        log.info("workId:{},plainText:{}", new Object[]{workId, plainText});
        T o = jsonParse(plainText, workId, clazz);
        //validateBean(o,workId);
        return o;

    }


    @NonNull
    public String postHandle(Object resp, String partnerAESKey, String partnerNo, String workId) {
        return contentUtil.postHandle(resp, partnerAESKey, partnerNo, workId);
    }
    @NonNull
    protected  BaseHeadRespDTO initRespDTO(BaseHeadReqDTO reqDto, String workId, int errCode, String errMsg) {
        HeadRespDTO head = new HeadRespDTO();
        head.setVersion(reqDto.getHead().getVersion());
        head.setCharset(reqDto.getHead().getCharset());
        head.setPartnerNo(reqDto.getHead().getPartnerNo());
        head.setPartnerType(reqDto.getHead().getPartnerType());
        head.setTxnCode(reqDto.getHead().getTxnCode());
        head.setTraceId(reqDto.getHead().getTraceId());
        head.setWorkId(workId);
        head.setRespDate(DateUtil.getDateyyyyMMdd());
        head.setRespTime(DateUtil.getDateyyyyMMddHHmmss());
        head.setRespCode(errCode);
        head.setRespMsg(errMsg);
        BaseHeadRespDTO respDto = new BaseHeadRespDTO();
        respDto.setHead(head);
        return respDto;
    }
}
